package com.jeeplus.modules.cms.front.web;

import com.jeeplus.cms.common.CmsConstant;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.cms.column.service.CmsColumnService;
import com.jeeplus.modules.cms.guestbook.entity.CmsGuestbook;
import com.jeeplus.modules.cms.guestbook.service.CmsGuestbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 留言板Controller
 *
 * @author LiGuanHua
 * @version 2017-11-01
 */
@Controller
@RequestMapping(value = "${frontPath}/guestbook")
public class FrontGuestbookController extends BaseController {

    @Autowired
    private CmsGuestbookService guestbookService;
    @Autowired
    private CmsColumnService columnService;

    @ModelAttribute
    public CmsGuestbook get(@RequestParam(required = false) String id) {
        CmsGuestbook entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = guestbookService.get(id);
        }
        if (entity == null) {
            entity = new CmsGuestbook();
        }
        return entity;
    }

    /**
     * 留言板-保存留言信息
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxJson save(CmsGuestbook guestbook, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) throws Exception {
        AjaxJson j = new AjaxJson();
        if (null == guestbook.getColumn() || StringUtils.isBlank(guestbook.getColumn().getId())) {
            j.setSuccess(false);
            j.setMsg("栏目ID为空，保存留言失败，请联系管理员！");
            return j;
        }

        if (StringUtils.isBlank(guestbook.getId()) && CmsConstant.YES.equals(columnService.get(guestbook.getColumn().getId()).getAuditFlag())) {
            guestbook.setDelFlag(CmsGuestbook.DEL_FLAG_AUDIT);
        }
        if (StringUtils.isBlank(guestbook.getName())) {
            guestbook.setName("匿名");
        }
        if (StringUtils.isBlank(guestbook.getTitle())) {
            String content = guestbook.getContent();
            guestbook.setTitle(content.substring(0, Math.min(content.length(), 30)) + (content.length() > 30 ? "..." : ""));
        }
        if (StringUtils.isBlank(guestbook.getId())) {
            guestbook.setAgree(0);
            guestbook.setOppose(0);
        }
        if (!beanValidator(model, guestbook)) {
            j.setSuccess(false);
            j.setMsg("保存失败，您的留言信息有误，请重新录入或联系管理员！");
            return j;
        }

        guestbook.setIp(request.getRemoteAddr());
        guestbookService.save(guestbook);
        j.setSuccess(true);
        j.setMsg("保存留言成功");
        return j;
    }

    /**
     * 留言板-点赞
     */
    @ResponseBody
    @RequestMapping(value = "agree")
    public AjaxJson agree(CmsGuestbook guestbook) throws Exception {
        AjaxJson j = new AjaxJson();
        if (null == guestbook || StringUtils.isBlank(guestbook.getId())) {
            j.setSuccess(false);
            j.setMsg("留言ID为空，点赞失败，请联系管理员！");
            return j;
        }
        guestbookService.agree(guestbook);
        j.setSuccess(true);
        j.setMsg("点赞成功");
        return j;
    }

    /**
     * 留言板-反对
     */
    @ResponseBody
    @RequestMapping(value = "oppose")
    public AjaxJson oppose(CmsGuestbook guestbook) throws Exception {
        AjaxJson j = new AjaxJson();
        if (null == guestbook || StringUtils.isBlank(guestbook.getId())) {
            j.setSuccess(false);
            j.setMsg("留言ID为空，反对失败，请联系管理员！");
            return j;
        }
        guestbookService.oppose(guestbook);
        j.setSuccess(true);
        j.setMsg("反对成功");
        return j;
    }

}
